package com.inkyi.assistant.app.component.rsyntax.action;

import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.Theme;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ChangeThemeAction implements ActionListener {

    private static final Map<String,String> nameFileMap = new HashMap<>();
    static {
        nameFileMap.put("默认主题","default.xml");
        nameFileMap.put("默认主题 (跟随系统)","default-alt.xml");
        nameFileMap.put("Dark","dark.xml");
        nameFileMap.put("Druid","druid.xml");
        nameFileMap.put("Monokai","monokai.xml");
        nameFileMap.put("Eclipse","eclipse.xml");
        nameFileMap.put("IDEA","idea.xml");
        nameFileMap.put("Visual Studio","vs.xml");
    }

    public static List<String> getThemeKey(){
        Set<String> keySet = nameFileMap.keySet();
        return keySet.stream().sorted().toList();
    }

    private final RSyntaxTextArea textArea;

    public ChangeThemeAction(RSyntaxTextArea textArea) {
        this.textArea = textArea;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JComboBox<String> source = (JComboBox<String>) e.getSource();
        String selectedItem = (String) source.getSelectedItem();
        String xml = nameFileMap.get(selectedItem);
        InputStream in = ChangeThemeAction.class.getResourceAsStream("/rsyntaxtextarea/themes/" + xml);
        try {
            Theme theme = Theme.load(in);
            theme.apply(textArea);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

}
